package com.example.gss.controller;

import com.example.gss.entity.Scholarship;
import com.example.gss.Service.ScholarshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/scholarships")
public class ScholarshipController {

    @Autowired
    private ScholarshipService scholarshipService;

    // 获取所有奖学金
    @GetMapping
    public ResponseEntity<List<Scholarship>> getAll() {
        return new ResponseEntity<>(scholarshipService.getAll(), HttpStatus.OK);
    }

    // 根据ID获取奖学金
    @GetMapping("/{id}")
    public ResponseEntity<Scholarship> getById(@PathVariable String id) {
        Scholarship scholarship = scholarshipService.getById(id);
        return scholarship != null ?
                new ResponseEntity<>(scholarship, HttpStatus.OK) :
                new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // 添加奖学金
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Scholarship scholarship) {
        int result = scholarshipService.save(scholarship);
        return new ResponseEntity<>(result, result > 0 ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST);
    }

    // 更新奖学金
    @PutMapping
    public ResponseEntity<Integer> update(@RequestBody Scholarship scholarship) {
        int result = scholarshipService.update(scholarship);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    // 删除奖学金
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(@PathVariable String id) {
        int result = scholarshipService.delete(id);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    // 按年份查询
    @GetMapping("/year/{year}")
    public ResponseEntity<List<Scholarship>> getByYear(@PathVariable Integer year) {
        return new ResponseEntity<>(scholarshipService.getByYear(year), HttpStatus.OK);
    }

    // 按级别查询
    @GetMapping("/level/{level}")
    public ResponseEntity<List<Scholarship>> getByLevel(@PathVariable String level) {
        return new ResponseEntity<>(scholarshipService.getByLevel(level), HttpStatus.OK);
    }
}